package ru.niir.goodfil.c1;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

import java.util.ArrayList;
import java.util.List;

@XStreamAlias("Document")
public class Document {
    @XStreamAsAttribute
    private String type;

    @XStreamAlias("Response")
    private Response response;

    @XStreamAlias("Clients")
    private List<Client> clients;

    @XStreamAlias("Rests")
    private List<Rest> rests;

    @XStreamAlias("Orders")
    private List<Order> orders;

    @XStreamAlias("Goods")
    private List<Good> goods;


    public void trim() {
        if (clients != null && clients.size() == 0) clients = null;
        if (rests != null && rests.size() == 0) rests = null;
        if (orders != null && orders.size() == 0) orders = null;
        if (goods != null && goods.size() == 0) goods = null;

    }

    /**
     * Конструктор без параметров для XStream
     */
    private Document() {
    }

    /**
     * Конструктор с указанием типа документа
     *
     * @param type тип докупмента
     */
    private Document(String type) {
        this.type = type;
    }

    /**
     * Создать документ, указав его тип
     *
     * @param type тип документа (DocumentType)
     * @return пустой документ
     * @throws ExceptionInInitializerError в случае, если переданный тип документа неверен
     * @see ru.niir.goodfil.c1.DocumentType
     */
    public static Document create(String type) {
        if (!DocumentType.validate(type)) throw new ExceptionInInitializerError("Bad document type: " + type);
        return new Document(type);
    }

    /**
     * Получить тип документа
     */
    public String getType() {
        return type;
    }

    /**
     * Установить тип документа
     */
    public void setType(String type) {
        this.type = type;
    }

    public List<Client> getClients() {
        if (clients == null) clients = new ArrayList<Client>();
        return clients;
    }

    public void setClients(List<Client> clients) {
        this.clients = clients;
    }

    public List<Rest> getRests() {
        if (rests == null) rests = new ArrayList<Rest>();
        return rests;
    }

    public void setRests(List<Rest> rests) {
        this.rests = rests;
    }

    public List<Order> getOrders() {
        if (orders == null) orders = new ArrayList<Order>();
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }

    public List<Good> getGoods() {
        if (goods == null) goods = new ArrayList<Good>();
        return goods;
    }

    public void setGoods(List<Good> goods) {
        this.goods = goods;
    }

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }

}
